﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using SWEN3Application.Classes;
using System.Collections;

namespace SWEN3Application
{
    public partial class RoomManagement : MasterForm
    {
        Rooms r;

        public RoomManagement()
        {
            InitializeComponent();
            CreateRoomTypeComboBox.SelectedIndex = 0;
        }


        private void RoomTabControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (RoomTabControl.SelectedIndex == 1)
            {
                ManageRoomDataGridView.Rows.Clear();
                ManageRoomDataGridView.ColumnCount = 4;
                ManageRoomDataGridView.Columns[0].Name = "Room Number";
                ManageRoomDataGridView.Columns[1].Name = "Room Rates";
                ManageRoomDataGridView.Columns[2].Name = "Room Type";
                ManageRoomDataGridView.Columns[3].Name = "Room Status";

                ArrayList roomList = RoomDBManager.GetAllRooms();
                Rooms r = new Rooms();

                for (int i = 0; i < roomList.Count; i++)
                {
                    r = (Rooms)roomList[i];
                    Object[] row = { r.RoomNo, r.RoomRates, r.RoomType, r.RoomStatus };
                    ManageRoomDataGridView.Rows.Add(row);
                }
                ManageRoomDataGridView.Refresh();

                if (ManageRoomDataGridView.DisplayedRowCount(false) == 0)
                {
                    MessageBox.Show("No room have been created yet.");
                }
            }
        }

        private void RoomNo_TextChanged(object sender, EventArgs e)
        {
            if (Regex.IsMatch(UpdateRoomNo.Text, @"^\d+$"))
            {
                r = RoomDBManager.GetRoomByRoomNo(Convert.ToInt32(UpdateRoomNo.Text));
                if (r != null)
                {
                    errorProvider.Clear();
                    UpdateRoomInformationBox.Visible = true;
                    setData();
                }
                else
                {
                    errorProvider.SetError(UpdateRoomNo, "Room Number not found!");
                    UpdateRoomInformationBox.Visible = false;
                }
            }
            else
            {
                errorProvider.SetError(UpdateRoomNo, "Invalid Room Number format!");
                UpdateRoomInformationBox.Visible = false;
            }
        }

        private void setData()
        {
            UpdateRoomNo.Text = r.RoomNo.ToString();
            UpdateRoomStatus.Text = r.RoomStatus;
            UpdateRoomTypeComboBox.SelectedItem= r.RoomType;
            UpdateRoomRates.Text = r.RoomRates.ToString();
            UpdateCheckIn.Text = r.CheckIn.ToString();
            UpdateCheckOut.Text = r.CheckOut.ToString();
        }

        private void Reset_Click(object sender, EventArgs e)
        {
            setData();
        }

        private void Update_Click(object sender, EventArgs e)
        {
            try
            {
                if (CustomValidateForUpdate())
                {
                    Rooms updatedRooms = new Rooms();
                    updatedRooms.RoomNo = Convert.ToInt32(UpdateRoomNo.Text);
                    updatedRooms.RoomStatus = UpdateRoomStatus.Text;
                    updatedRooms.RoomType = UpdateRoomTypeComboBox.SelectedItem.ToString();
                    updatedRooms.RoomRates = Convert.ToDouble(UpdateRoomRates.Text);
                    updatedRooms.CheckIn = Convert.ToDateTime(UpdateCheckIn.Text);
                    updatedRooms.CheckOut = Convert.ToDateTime(UpdateCheckOut.Text);

                    housekeepingOscar h = HousekeepingDBManager.GetRoomByRoomNo(updatedRooms.RoomNo);
                    h.roomStatus = updatedRooms.RoomStatus;
                    h.roomType = updatedRooms.RoomType;
                    HousekeepingDBManager.UpdateHousekeeping(h);

                    if (RoomDBManager.updateRooms(updatedRooms))
                    {
                        r = updatedRooms;
                        MessageBox.Show("Update Successful!");
                    }
                    else
                    {
                        MessageBox.Show("Update Room Fail!");
                    }
                }
            }
            //This is to catch all types of exception that is not handled by the if else. 
            //Also to ensure the application is still running.
            catch (Exception ex)
            {
                MessageBox.Show("A serious error has occured during updating room process.");
            }
        }

        private Boolean CustomValidateForUpdate()
        {
            Boolean isValid = true;
            errorProvider.Clear();
            if (String.IsNullOrEmpty(UpdateRoomNo.Text))
            {
                isValid = false;
                errorProvider.SetError(UpdateRoomNo, "Room No field is empty.");
            }
            if (String.IsNullOrEmpty(UpdateRoomRates.Text))
            {
                isValid = false;
                errorProvider.SetError(UpdateRoomRates, "The field is empty!");
            }
            if (!Regex.IsMatch(UpdateRoomRates.Text, @"(\d+.\d{2})|\d"))
            {
                isValid = false;
                errorProvider.SetError(UpdateRoomRates, "Room Rates is not in numeric format.");
            }
            return isValid;
        }

        private void FindRoomNo_TextChanged(object sender, EventArgs e)
        {
            if (Regex.IsMatch(FindRoomNo.Text, @"^\d+$"))
            {
                ManageRoomDataGridView.ClearSelection();
                for (int i = 0; i < ManageRoomDataGridView.RowCount; i++)
                {
                    int roomNo = Convert.ToInt32(ManageRoomDataGridView.Rows[i].Cells[0].Value);
                    if (roomNo == Convert.ToInt32(FindRoomNo.Text))
                    {
                        ManageRoomDataGridView.Rows[i].Selected = true;
                        ManageRoomDataGridView.FirstDisplayedScrollingRowIndex = i;
                        break;
                    }
                }
            }
        }

        private void ManageRoomDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                RoomTabControl.SelectedIndex = 2;
                UpdateRoomNo.Text = ManageRoomDataGridView[0, e.RowIndex].Value.ToString();
            }
        }

        private void Clear_Click(object sender, EventArgs e)
        {
            CreateRoomNo.Text = "";
            CreateRoomRates.Text = "";
        }

        private void Create_Click(object sender, EventArgs e)
        {
            try
            {
                if (CustomValidateForCreate())
                {
                    DateTime Default = DateTime.Parse("1/1/2001");
                    Rooms newRooms = new Rooms();
                    newRooms.RoomNo = Convert.ToInt32(CreateRoomNo.Text);
                    newRooms.RoomStatus = CreateRoomStatus.Text;
                    newRooms.RoomType = CreateRoomTypeComboBox.SelectedItem.ToString();
                    newRooms.RoomRates = Convert.ToDouble(CreateRoomRates.Text);
                    newRooms.CheckIn = DateTime.MinValue;
                    newRooms.CheckOut = DateTime.MinValue;

                    housekeepingOscar h = new housekeepingOscar();
                    h.roomNo = newRooms.RoomNo;
                    h.roomStatus = newRooms.RoomStatus;
                    h.roomType = newRooms.RoomType;
                    h.inCharge = "";
                    h.cleaningStartTime = Default;
                    h.cleaningEndTime = Default;
                    HousekeepingDBManager.InsertHousekeeping(h);

                    if (RoomDBManager.createRoom(newRooms))
                    {
                        r = newRooms;
                        MessageBox.Show("Create Successful!");
                    }
                    else
                    {
                        MessageBox.Show("Create Room Fail!");
                    }
                }
            }
            //This is to catch all types of exception that is not handled by the if else. 
            //Also to ensure the application is still running.
            catch (Exception ex)
            {
                MessageBox.Show("A serious error has occured during creating room process.");
            }
        }

        private Boolean CustomValidateForCreate()
        {
            Boolean IsValid = true;
            errorProvider.Clear();
            if (String.IsNullOrEmpty(CreateRoomNo.Text))
            {
                IsValid = false;
                errorProvider.SetError(CreateRoomNo, "The field is empty.");
            }
            else if (!Regex.IsMatch(CreateRoomNo.Text, @"^\d+$"))
            {
                IsValid = false;
                errorProvider.SetError(CreateRoomNo, "Room No is not in numeric format.");
            }
            else if (RoomDBManager.GetRoomByRoomNo(Convert.ToInt32(CreateRoomNo.Text)) != null)
            {
                IsValid = false;
                errorProvider.SetError(CreateRoomNo, "This Room No is in use!");
            }

            if (String.IsNullOrEmpty(CreateRoomRates.Text))
            {
                IsValid = false;
                errorProvider.SetError(CreateRoomRates, "The field is empty!");
            }
            else if (!Regex.IsMatch(CreateRoomRates.Text, @"^\d*.\d{1,2}\z|^\d+$"))
            {
                IsValid = false;
                errorProvider.SetError(CreateRoomRates, "Room Rates is not in correct format!");
            }
            return IsValid;
        }
    }
}
